// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'

// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
import { asyncRoutes } from '@/router'
import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from './utils/get-page-title'

// 未登录也能访问到的页面
const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const token = store.state.user.token
  if (token) {
    // 优化每次路由跳转都要发请求
    if (!store.getters.userId) {
      // 这里要注意,只有拿到用户信息之后才让跳转
      const { data: { roles }} = await store.dispatch('user/getUserInfoAction')
      // 服务器返回的这个角色有的权限
      console.log(roles.menus)// 数组 不确定可能是不定数
      console.log(asyncRoutes)// 数组9个
      const filterRoutes = asyncRoutes.filter(item => {
        // return true/false
        return roles.menus.includes(item.name)
      }) // 筛选后的路由
      // console.log(filterRoutes)
      // 404 page must be placed at the end !!!

      store.commit('user/setRoutes', filterRoutes)

      router.addRoutes([...filterRoutes, { path: '*', redirect: '/404', hidden: true }])// 添加动态路由
      next(to.path)
      console.log(router)
    }

    if (to.path === '/login') {
      NProgress.done()
      next('/')
    } else {
      // 发现一个问题:网速慢,会导致layout组件加载不出来,结论:layout也是在服务器上面的,所以我们想要渲染layout结构,也是需要发请求的
      // alert('放行')
      next()
    }
  } else {
    // 或者 whiteList.includes(to.path) 来判断to.path是否在白名单中
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      NProgress.done()
      next('/login')
    }
  }
})

router.beforeEach((to, from, next) => {
  // 动态修改网页title 3.0版本--前置路由守卫
  document.title = getPageTitle(to.meta.title)
  next()
})

router.afterEach((to, from) => {
  NProgress.done()
  // 动态修改网页title 1.0版本
  // document.title = to.meta.title + '-niu的人资'

  // 动态修改网页title 2.0版本--使用花裤衩封装的方法
  // document.title = getPageTitle(to.meta.title)
})
